export async function hydrateAgentPrompts(agentConfig: any): Promise<any> {
  if (!agentConfig?.agents?.length) {
    return agentConfig;
  }

  const agents = await Promise.all(
    agentConfig.agents.map(async (agent: any) => {
      if (Array.isArray(agent.system_prompt) && agent.system_prompt.length) {
        return agent;
      }

      const promptPath = agent.system_prompt_path || agent.systemPromptPath;
      if (typeof promptPath === 'string' && promptPath.trim()) {
        try {
          const response = await fetch(chrome.runtime.getURL(promptPath));
          const text = await response.text();
          agent.system_prompt = text.replace(/\r/g, '').split('\n');
        } catch (error) {
          console.error(`[PromptLoader] 无法加载 system prompt: ${promptPath}`, error);
          agent.system_prompt = [];
        }
      } else if (typeof agent.system_prompt === 'string') {
        agent.system_prompt = agent.system_prompt.replace(/\r/g, '').split('\n');
      }

      return agent;
    })
  );

  return { ...agentConfig, agents };
}
